package com.kvest.maptest.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.*;
import com.kvest.maptest.R;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 05.09.13
 * Time: 18:57
 * To change this template use File | Settings | File Templates.
 */
public class MarkersMapActivity extends FragmentActivity {
    private GoogleMap map;
    private Handler handler;
    private Runnable runnable;
    private int radius;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_map);

        init();
    }

    @Override
     public void onPause() {
        super.onPause();

        handler.removeCallbacks(runnable);
    }

    @Override
    public void onResume() {
        super.onResume();

        handler.postDelayed(runnable, 16);
    }

    private void init() {
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(46.444200, 30.756184), 15.0f));
        map.setMyLocationEnabled(true);

        final Marker m =  map.addMarker(new MarkerOptions()
                            .position(new LatLng(46.448300, 30.747852))
                            .title("Hello world")
                            .snippet("PThis is snippet")
                            .draggable(true)
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

        CircleOptions circleOptions = new CircleOptions()
                .center(m.getPosition())
                .radius(50)
                .strokeColor(Color.GREEN); // In meters

        // Get back the mutable Circle
        final Circle circle = map.addCircle(circleOptions);

        map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker marker) {
                if (m.equals(marker)) {
                    circle.setStrokeColor(Color.RED);
                }
            }

            @Override
            public void onMarkerDrag(Marker marker) {
                if (m.equals(marker)) {
                    circle.setCenter(marker.getPosition());
                }
            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                if (m.equals(marker)) {
                    circle.setStrokeColor(Color.GREEN);
                }
            }
        });

        radius = 0;
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                radius += 4;
                if (radius > 50) {
                    radius = 0;
                }
                circle.setRadius(20 + radius);
                circle.setStrokeWidth(1 + 7 * (radius / 50f));
                handler.postDelayed(this, 16);
            }
        };
    }
}
